package cn.softgame.jserver.application.chat;

import cn.softgame.jserver.application.Connection;
import cn.softgame.jserver.application.connection.ConnectionFactory;

public class ChatClient implements Runnable {
	private ConnectionFactory connFactory = ConnectionFactory.getInstance(ConnectionFactory.CLIENT);

	@Override
	public void run() {
		try{
			String url = "10.0.0.17";
			int port = 81;
			for(int i=200;i<10000;i++){
				//tcpConnection(url,port);
				udpConnection(url,i);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	private void tcpConnection(String url, int port) throws Exception {
		Connection conn = connFactory.getTcpConnection(false);
		conn.open(url, port);
		for(int i=0;i<1;i++){
			 String msg = i + "helloI 碰 am xuquan \r\n" + "this is two line" + i;
			 conn.writeString(msg);
		}
		System.out.println(" send TCP ok ................");
		
		for(int i=0;i<1;i++){
			String result = conn.readString();
			System.out.println(" TCP Response result =" + result);
		}
		conn.close();
	}

	private void udpConnection(String url, int port) throws Exception {
		Connection conn = connFactory.getUdpConnection();
		conn.open(url, port);
		for(int i=5;i<8;i++){
			String msg = i + "helloI 碰 am xuquan \r\n" + "this is two line" + i;
		    conn.writeString(msg);
		}
		System.out.println(" send UDP ok ....................");
		
		for (int i = 5; i < 8; i++) {
			String result = conn.readString();
			System.out.println("get UDP Response result =" + result);
		}
		conn.close();
	}

	public static void main(String[] args) {
		Thread chat = new Thread(new ChatClient());
		chat.start();
	}
}
